#include "initdata.h"
InitData* InitData::m_instance = nullptr;
QMutex InitData::m_mutex;
InitData *InitData::getInstance()
{
    if(!m_instance)
    {
        QMutexLocker locker(&m_mutex);
        if(!m_instance)
        {
            m_instance = new InitData();
        }
    }
    return m_instance;
}

void InitData::init_Data()
{
    // qDebug()<<"构造函数触发!";
    QString currentDir = QCoreApplication::applicationDirPath();
    // qDebug()<<"当前程序位置: "<<currentDir;
    QString fileName = QString("%1/config.ini").arg(currentDir);

    QSettings configIni(fileName, QSettings::IniFormat);

    configIni.beginGroup("Scanner_Parameters");
    setScanner_Port(configIni.value("Scanner_Port").toString());
    setScanner_BaudRate(configIni.value("Scanner_BaudRate").toString());
    setScanner_DataBit(configIni.value("Scanner_DataBit").toString());
    setScanner_Parity(configIni.value("Scanner_Parity").toString());
    setScanner_StopBit(configIni.value("Scanner_StopBit").toString());
    configIni.endGroup();


    configIni.beginGroup("Printer_Parameters");
    setprinter_Port(configIni.value("Printer_Port").toString());
    setprinter_BaudRate(configIni.value("Printer_BaudRate").toString());
    setprinter_DataBit(configIni.value("Printer_DataBit").toString());
    setprinter_Parity(configIni.value("Printer_Parity").toString());
    setprinter_StopBit(configIni.value("Printer_StopBit").toString());
    configIni.endGroup();
}

void InitData::save_Data()
{
    // qDebug()<<"析构函数触发!";
    QString currentDir = QCoreApplication::applicationDirPath();
    // qDebug()<<"当前程序位置: "<<currentDir;

    QString fileName = QString("%1/config.ini").arg(currentDir);
    QSettings configIni(fileName, QSettings::IniFormat);

    configIni.beginGroup("Scanner_Parameters");
    configIni.setValue("Scanner_Port",scanner_Port());
    configIni.setValue("Scanner_BaudRate",scanner_BaudRate());
    configIni.setValue("Scanner_DataBit",scanner_DataBit());
    configIni.setValue("Scanner_Parity",scanner_Parity());
    configIni.setValue("Scanner_StopBit",scanner_StopBit());
    configIni.endGroup();

    configIni.beginGroup("Printer_Parameters");
    configIni.setValue("Printer_Port",printer_Port());
    configIni.setValue("Printer_BaudRate",printer_BaudRate());
    configIni.setValue("Printer_DataBit",printer_DataBit());
    configIni.setValue("Printer_Parity",printer_Parity());
    configIni.setValue("Printer_StopBit",printer_StopBit());
    configIni.endGroup();

}

InitData::InitData(QObject *parent)
    : QObject{parent}
{
    init_Data();
}

InitData::~InitData()
{
    save_Data();
}

QString InitData::scanner_Port() const
{
    return m_scanner_Port;
}

void InitData::setScanner_Port(const QString &newScanner_Port)
{
    if (m_scanner_Port == newScanner_Port)
        return;
    m_scanner_Port = newScanner_Port;
    emit scanner_PortChanged();
}

QString InitData::scanner_BaudRate() const
{
    return m_scanner_BaudRate;
}

void InitData::setScanner_BaudRate(const QString &newScanner_BaudRate)
{
    if (m_scanner_BaudRate == newScanner_BaudRate)
        return;
    m_scanner_BaudRate = newScanner_BaudRate;
    emit scanner_BaudRateChanged();
}

QString InitData::scanner_DataBit() const
{
    return m_scanner_DataBit;
}

void InitData::setScanner_DataBit(const QString &newScanner_DataBit)
{
    if (m_scanner_DataBit == newScanner_DataBit)
        return;
    m_scanner_DataBit = newScanner_DataBit;
    emit scanner_DataBitChanged();
}

QString InitData::scanner_Parity() const
{
    return m_scanner_Parity;
}

void InitData::setScanner_Parity(const QString &newScanner_Parity)
{
    if (m_scanner_Parity == newScanner_Parity)
        return;
    m_scanner_Parity = newScanner_Parity;
    emit scanner_ParityChanged();
}

QString InitData::scanner_StopBit() const
{
    return m_scanner_StopBit;
}

void InitData::setScanner_StopBit(const QString &newScanner_StopBit)
{
    if (m_scanner_StopBit == newScanner_StopBit)
        return;
    m_scanner_StopBit = newScanner_StopBit;
    emit scanner_StopBitChanged();
}

QString InitData::printer_Port() const
{
    return m_printer_Port;
}

void InitData::setprinter_Port(const QString &newPrinter_Port)
{
    if (m_printer_Port == newPrinter_Port)
        return;
    m_printer_Port = newPrinter_Port;
    emit printer_PortChanged();
}

QString InitData::printer_BaudRate() const
{
    return m_printer_BaudRate;
}

void InitData::setprinter_BaudRate(const QString &newPrinter_BaudRate)
{
    if (m_printer_BaudRate == newPrinter_BaudRate)
        return;
    m_printer_BaudRate = newPrinter_BaudRate;
    emit printer_BaudRateChanged();
}

QString InitData::printer_DataBit() const
{
    return m_printer_DataBit;
}

void InitData::setprinter_DataBit(const QString &newPrinter_DataBit)
{
    if (m_printer_DataBit == newPrinter_DataBit)
        return;
    m_printer_DataBit = newPrinter_DataBit;
    emit printer_DataBitChanged();
}

QString InitData::printer_Parity() const
{
    return m_printer_Parity;
}

void InitData::setprinter_Parity(const QString &newPrinter_Parity)
{
    if (m_printer_Parity == newPrinter_Parity)
        return;
    m_printer_Parity = newPrinter_Parity;
    emit printer_ParityChanged();
}

QString InitData::printer_StopBit() const
{
    return m_printer_StopBit;
}

void InitData::setprinter_StopBit(const QString &newPrinter_StopBit)
{
    if (m_printer_StopBit == newPrinter_StopBit)
        return;
    m_printer_StopBit = newPrinter_StopBit;
    emit printer_StopBitChanged();
}
